#!/bin/bash

set -e
pushd $(cd $(dirname ${0})/..; pwd) > /dev/null

case "${OSTYPE}" in
    msys*) python="winpty python";;
    *) python="python";;
esac

bold() {
    echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)" 
}

venv() {
    env_dir=./third_party/env/pytorch
    [ -d "${env_dir}" ] || ${python} -m venv ${env_dir}
    case "${OSTYPE}" in
        msys*) source ${env_dir}/Scripts/activate;;
        *) source ${env_dir}/bin/activate;;
    esac
    ${python} -m pip install --quiet --upgrade pip
}

clean() {
    bold "pytorch clean"
    rm -rf "./third_party/env/pytorch"
    rm -rf "./third_party/src/pytorch"
    rm -rf "./third_party/src/torchvision"
}

sync() {
    bold "pytorch sync"
    [ -d "./third_party/src/pytorch" ] || git clone --quiet --recursive https://github.com/pytorch/pytorch.git "./third_party/src/pytorch"
    pushd "./third_party/src/pytorch" > /dev/null
    git pull --quiet --prune
    git submodule sync --quiet
    git submodule update --quiet --init --recursive
    popd > /dev/null
    [ -d "./third_party/src/torchvision" ] || git clone --quiet --recursive https://github.com/pytorch/vision.git "./third_party/src/torchvision"
    pushd "./third_party/src/torchvision" > /dev/null
    git pull --quiet --prune
    popd > /dev/null
}

install() {
    bold "pytorch install"
    venv
    ${python} -m pip install --quiet --upgrade future protobuf scipy
    ${python} -m pip install --quiet --upgrade --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
    deactivate
}

schema() {
    bold "caffe2 schema"
    [[ $(grep -U $'\x0D' ./src/caffe2-proto.js) ]] && crlf=1
    npx pbjs -t static-module -w closure --no-encode --no-delimited --no-comments --no-convert --no-verify --no-create --keep-case --decode-text -r caffe2 -o ./src/caffe2-proto.js ./third_party/src/pytorch/caffe2/proto/caffe2.proto
    node ./tools/update_pbjs.js enumeration ./src/caffe2-proto.js floats float 1
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./src/caffe2-proto.js ./src/caffe2-proto.js
    fi
}

metadata() {
    [[ $(grep -U $'\x0D' ./src/pytorch-metadata.json) ]] && crlf=1
    venv
    export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
    bold "pytorch metadata"
    ${python} ./tools/pytorch-script.py metadata
    bold "caffe2 metadata"
    ${python} ./tools/caffe2-script.py metadata
    deactivate
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./src/pytorch-metadata.json ./src/pytorch-metadata.json
        unix2dos --quiet --newfile ./src/caffe2-metadata.json ./src/caffe2-metadata.json
    fi
}

zoo() {
    bold "pytorch zoo"
    venv
    export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
    ${python} ./tools/pytorch-script.py zoo
    deactivate
}

while [ "$#" != 0 ]; do
    command="$1" && shift
    case "${command}" in
        "clean") clean;;
        "sync") sync;;
        "install") install;;
        "schema") schema;;
        "metadata") metadata;;
        "zoo") zoo;;
    esac
done
